﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bzure.Common.Data;
using Bzure.Security.Principal;
using Bzure.Common.Core;

namespace Bzure.Apps.Security.DataAccess
{
    public class UserGroupRepository : RepositoryBase
    {
        static UserGroupRepository()
        {
            UserGroup entity = new UserGroup();
            Type type = entity.GetType();
            AppendDataMap(type, "Description", () => entity.Description);
            AppendDataMap(type, "ID", () => entity.ID);
            AppendDataMap(type, "Name", () => entity.Name);
            AppendDataMap(type, "State", () => entity.State);
            AppendDataMap(type, "OwnerName", () => entity.OwnerName);
            AppendDataMap(type, "BlockName", () => entity.BlockName);
            ResultMapDelegate<UserGroup> queryMap = UserGroup_Map;
            QueryMapContainer[entity.GetType()] = queryMap;



        }
        public UserGroupRepository(Bzure.Common.Transactions.ITransactionSupport contractProvider)
            : base(contractProvider)
        {

        }
        public override string TableName
        {
            get
            {
                return "T_Security_UserGroups";
            }
        }
        /// <summary>
        /// 用户实体数据映射
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static UserGroup UserGroup_Map(DataReader reader)
        {
            UserGroup BzureUser = new UserGroup()
            {
                Description = reader.GetStringNullable("Description"),
                ID = reader.GetStringNullable("ID"),
                Name = reader.GetStringNullable("Name"),
                OwnerName = reader.GetStringNullable("OwnerName"),
                BlockName = reader.GetStringNullable("BlockName"),
                State = reader.GetInt("State")
            };
            return BzureUser;
        }
        public UserGroup[] GetUserGroups(string[] GroupIDs)
        {
            if (GroupIDs.IsNullOrEmpty())
                return default(UserGroup[]);
            string s = string.Join("','", GroupIDs);
            s = string.Format("'{0}'", s);


            string sql = string.Format("SELECT * FROM  {1} WHERE ID IN({0})", s, this.TableName);
            return new SqlEntityMap<UserGroup>(this.ContractProvider, sql)
                .SetMap(UserGroup_Map)
                .AsList().ToArray();
        }
    }
}
